package com.example.timertask_demo;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/3/16.
 */

public class WaitingFragment extends Fragment {

    private Timer mTimer = new Timer();
    int i=10;
    private TextView mText;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    mText.setText(""+i);
                    if (i<0){
                        mTimer.cancel();
                        mText.setVisibility(View.GONE);
                    }
                    break;
            }
        }
    };


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.waiting_dialog, container, false);
        mText = (TextView) view.findViewById(R.id.wait_textview);

        mTimer.schedule(mTimerTask,1000,1000);

        return view;
    }

    TimerTask mTimerTask = new TimerTask() {
        @Override
        public void run() {
            i--;
            Message message = new Message();
            message.what=1;
            mHandler.sendMessage(message);
        }
    };

}
